package com.zhiyi.coco.filter;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CrossInterceptorFile implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        // 跨域资源共享（ cors ）
        String origin = httpServletRequest.getHeader("Origin");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", origin);
        //允许的方法
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
        //允许的头部参数
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin,Content-Type,Accept,X-os,X-uid,X-token,X-role,X-Requested-With");
        //用户代理是否应该在跨域请求的情况下从其他域发送cookies
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        return true;
    }
}
